package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"tianlian-ttc/internal/app/api/model"
	"tianlian-ttc/internal/app/api/service"
	"time"
)

// TaskHandler 任务处理器
type TaskHandler struct {
	taskService *service.TaskService
}

// NewTaskHandler 创建任务处理器
func NewTaskHandler(taskService *service.TaskService) *TaskHandler {
	return &TaskHandler{
		taskService: taskService,
	}
}

// ApplyTask 处理任务申请
func (h *TaskHandler) ApplyTask(c *gin.Context) {
	var req model.TaskRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: http.StatusBadRequest,
			Msg:  "Invalid request format: " + err.Error(),
			Data: nil,
		})
		return
	}

	// 调用服务处理任务申请
	resp, err := h.taskService.ApplyTask(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: http.StatusInternalServerError,
			Msg:  "Failed to process task application: " + err.Error(),
			Data: nil,
		})
		return
	}

	c.JSON(http.StatusOK, resp)
}

// CancelTask handles task cancellation requests
func (h *TaskHandler) CancelTask(c *gin.Context) {
	var req model.TaskCancelRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: http.StatusBadRequest,
			Msg:  "Invalid request format: " + err.Error(),
			Data: nil,
		})
		return
	}

	// Validate each plan in the list
	for _, plan := range req.PlanList {
		if plan.PlanID == "" || plan.Satellite == "" || len(plan.TaskCancelList) == 0 {
			c.JSON(http.StatusBadRequest, model.Response{
				Code: http.StatusBadRequest,
				Msg:  "Missing required fields in plan: planID, satellite, or taskCancelList",
				Data: nil,
			})
			return
		}
	}

	if err := h.taskService.CancelTask(&req); err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: http.StatusInternalServerError,
			Msg:  "Failed to cancel task: " + err.Error(),
			Data: nil,
		})
		return
	}

	c.JSON(http.StatusOK, model.Response{
		Code: 0,
		Msg:  "success",
		Data: nil,
	})
}

// QueryIdleArc handles idle arc query requests
func (h *TaskHandler) QueryIdleArc(c *gin.Context) {
	var req model.IdleArcQueryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: http.StatusBadRequest,
			Msg:  "Invalid request format: " + err.Error(),
			Data: nil,
		})
		return
	}

	// Validate required fields
	if req.Satellite == "" ||
		req.StartTime == "" || req.EndTime == "" {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: http.StatusBadRequest,
			Msg:  "Missing required fields: originator, recipient, satellite, startTime, or endTime",
			Data: nil,
		})
		return
	}

	// Validate time format
	layout := "2006-01-02T15:04:05.000Z"
	if _, err := time.Parse(layout, req.StartTime); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: http.StatusBadRequest,
			Msg:  "Invalid startTime format. Expected format: 2020-12-08T00:01:04.000Z",
			Data: nil,
		})
		return
	}
	if _, err := time.Parse(layout, req.EndTime); err != nil {
		c.JSON(http.StatusBadRequest, model.Response{
			Code: http.StatusBadRequest,
			Msg:  "Invalid endTime format. Expected format: 2020-12-08T00:01:04.000Z",
			Data: nil,
		})
		return
	}

	resp, err := h.taskService.QueryIdleArc(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, model.Response{
			Code: http.StatusInternalServerError,
			Msg:  "Failed to query idle arcs: " + err.Error(),
			Data: nil,
		})
		return
	}

	c.JSON(http.StatusOK, resp)
}
